package com.example.buildtable.controller;

import com.example.buildtable.pojo.User;
import com.example.common.util.R;
import com.example.buildtable.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@Tag(name = "管理员账号")
@RequestMapping(value = "/user/")
public class UserController {
    @Autowired
    private UserService userService;

    @Operation(summary = "查询所有管理员数据")
    @GetMapping(value = "/getAllUser")
    public R getAllUser() {
        List<User> userList = userService.list();
        return R.ok("查询所有管理员数据成功").data("userList", userList);
    }

    @Operation(summary = "登录管理员")
    @PostMapping(value = "/login")
    public R login(@RequestBody  User admin) {
        Map<String, Object> map = userService.login(admin);
        if (map.get("state").equals("success")) {
            return R.ok("管理员登录成功").data("user", admin);
        } else {
            return R.error("管理员登录失败").data(map);
        }
    }

    @Operation(summary = "退出登录管理员")
    public R loginOut(HttpServletRequest request) {
        //清理session
        request.getSession().removeAttribute("user");
        if (request.getAttribute("user") == null) {
            return R.ok("管理员退出登录成功");
        } else {
            return R.error("管理员退出登录失败");
        }
    }

    @Operation(summary = "注册管理员")
    @PostMapping(value = "/addUser")
    public R addUser(@RequestBody User user) {
        boolean flag = userService.save(user);
        if (flag) {
            return R.ok("管理员注册成功").data("userId", user.getUserId());
        } else {
            return R.ok("管理员注册失败");
        }
    }

    @Operation(summary = "注销管理员")
    @GetMapping(value = "/deleteUser/{userId}")
    public R deleteUser(@PathVariable("userId") Integer userId) {
        boolean flag = userService.removeById(userId);
        if (flag) {
            return R.ok("管理员注销成功");
        } else {
            return R.ok("管理员注销失败");
        }
    }

    @Operation(summary = "按照管理员编号查看")
    @GetMapping(value = "/getUserById/{userId}")
    public R getUserById(@PathVariable("userId") Integer userId) {
        User user = userService.getById(userId);
        if (user != null) {
            return R.ok("按照管理员编号查看成功").data("user", user);
        } else {
            return R.error("按照管理员编号查看失败");
        }
    }

    @Operation(summary = "修改管理员")
    @PostMapping(value = "/updateUser")
    public R updateUser(User user) {
        boolean flag = userService.updateById(user);
        if (flag) {
            return R.ok("管理员修改成功");
        } else {
            return R.ok("管理员修改失败");
        }
    }

    @Operation(summary = "修改管理员的状态")
    @PostMapping(value = "/updateUserStatus")
    public R updateUserStatus(@RequestBody User user) {
        Integer status = user.getStatus();
        if (status == 1 || status == 0) {
            int newStatus = (status == 1) ? 0 : 1;
            user.setStatus(newStatus);
            boolean updated = userService.updateById(user);

            String statusText = null;
            if (updated) {
                statusText = (newStatus == 1) ? "可用" : "不可用";
                return R.ok(statusText + user.getUsername() + "成功").data("user", user);
            } else {
                return R.error(statusText + user.getUsername() + "失败");
            }
        } else {
            return R.error("无效的状态值");
        }
    }
}
